package com.example.android1.common.databus;

import android.util.Log;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import rx.functions.Func1;
import com.example.android1.mytaxi.common.databus.DataBusSubscriber;
import com.example.android1.mytaxi.common.databus.RegisterBus;
import com.example.android1.mytaxi.common.databus.RxBus;

/**
 * Created by liuguangli on 17/5/17.
 */
public class RxBusTest {
    public static final String TAG = "RxBusTest";
    Presenter presenter;

    @Before
    public void setUp() throws Exception {
        presenter = new Presenter(new Manager()); //初始化 presenter 并注册
        RxBus.getInstance().register(presenter); //注册presenter到RxBus。
    }

    @After
    public void tearDown() { //所有方法执行完后，解除注册
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        RxBus.getInstance().unRegister(presenter);
    }

    @Test
    public void testGetUser() throws Exception {
        presenter.getUser(); //模拟model请求后台数据
    }

    @Test
    public void testGetOrder() throws Exception {
        presenter.getOrder();
    }
}


/**
 * 模拟 Presenter
 */
class Presenter {
    private Manager manager;

    public Presenter(Manager manager) {
        this.manager = manager;
    }

    public void getUser() {
        manager.getUser();
    }

    public void getOrder() {
        manager.getOrder();
    }

    // 优化
    @RegisterBus
    public void onUser(User user) {
        Log.d(RxBusTest.TAG, "receive User in thread:" + Thread.currentThread());
    }
//    started: testGetUser(com.example.android1.common.databus.RxBusTest)
//    chainProcess getUser start in thread:Thread[RxCachedThreadScheduler-1,5,main] 子线程中处理
//    receive User in thread:Thread[main,5,main]  接受线程在主线程
//    finished: testGetUser(com.example.android1.common.databus.RxBusTest)

    @RegisterBus
    public void onOrder(Order order) {//
        Log.d(RxBusTest.TAG, "receive order in thread:" + Thread.currentThread());
    }
//    started: testGetOrder(com.example.android1.common.databus.RxBusTest)
//    chainProcess getOrder start in thread::Thread[RxCachedThreadScheduler-1,5,main]
//    receive order in thread:Thread[main,5,main]
//    finished: testGetOrder(com.example.android1.common.databus.RxBusTest)
}

/**
 * 模拟 MODEL,请求数据。
 */
class Manager {
    public void getUser() {
        RxBus.getInstance().chainProcess(new Func1() {
            @Override
            public Object call(Object o) {
                Log.d(RxBusTest.TAG, "chainProcess getUser start in thread:" + Thread.currentThread());
                User user = new User();
                try {
                    Thread.sleep(1000);//模拟耗时
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                // 把 User 数据到 Presenter。回调@RegisterBus 注解的 onUser方法。
                return user;
            }
        });
    }

    public void getOrder() {
        RxBus.getInstance().chainProcess(new Func1() {
            @Override
            public Object call(Object o) {
                Log.d(RxBusTest.TAG, "chainProcess getOrder start in thread::" + Thread.currentThread());
                Order order = new Order();
                // 把 order 数据到 Presenter.回调@RegisterBus 注解的 onUser方法。
                return order;
            }
        });
    }

}

/**
 * 要返回的数据类型
 */
class User {
}

class Order {
}